package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Alteration;
import visiopuce.objets.DGVVariants;

public class DGVVariantsDAOImpl extends GenericDAOImpl<DGVVariants, BigDecimal> implements DGVVariantsDAO {

	/** Constructeur priv� */
	private DGVVariantsDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static DGVVariantsDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static DGVVariantsDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new DGVVariantsDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<DGVVariants> getDGVVariantsByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<DGVVariants> dgvs = null;
		String sql = null;
		Query query;
		sql = "SELECT g FROM DGVVariants g WHERE g.chr LIKE :chromosome AND g.start IS NOT NULL AND g.start <= :fin AND g.end IS NOT NULL AND g.end >= :debut ORDER BY g.start";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		dgvs = (ArrayList<DGVVariants>) query.list();
		return dgvs;
	}
}